let ComFunJS={
    getNowDate: function (format, date) {
        if (!format) {
            format = "yyyy-MM-dd";
        }

        var now = new Date();
        if (date) {
            now = new Date(date);
        }

        var year = now.getFullYear();       //年
        var month = now.getMonth() + 1;     //月
        var day = now.getDate();            //日
        var hh = now.getHours();
        var mm = now.getMinutes();
        var ss = now.getSeconds();

        var clock = year + "-";

        switch (format) {
            case "yyyy-MM-dd": {
                if (month < 10)
                    clock += "0";
                clock += month + "-";
                if (day < 10) {
                    clock += "0";
                }
                clock += day;
            }
                break;
            case "yyyy-MM": {
                if (month < 10)
                    clock += "0";
                clock += month;
            }
                break;

            case "yyyy-MM-dd HH:mm:ss": {
                if (month < 10)
                    clock += "0";
                clock += month + "-";
                if (day < 10) {
                    clock += "0";
                }
                clock += day + " ";

                if (hh < 10)
                    clock += "0";
                clock += hh + ":";
                if (mm < 10)
                    clock += "0";
                clock += mm + ":";
                if (ss < 10)
                    clock += "0";
                clock += ss;
            }
                break;
            case "yyyy-MM-dd HH:mm": {
                if (month < 10)
                    clock += "0";
                clock += month + "-";
                if (day < 10) {
                    clock += "0";
                }
                clock += day + " ";

                if (hh < 10)
                    clock += "0";
                clock += hh + ":";
                if (mm < 10)
                    clock += "0";
                clock += mm;

            }
                break;                
            case "yyyyMMddHHmmss": {
                clock = year;
                if (month < 10)
                    clock += "0";
                clock += month;
                if (day < 10) {
                    clock += "0";
                }
                clock += day;

                if (hh < 10)
                    clock += "0";
                clock += hh;
                if (mm < 10)
                    clock += "0";
                clock += mm;
                if (ss < 10)
                    clock += "0";
                clock += ss;
            }
                break;
            case "MMddHHmm": {
                clock = "";
                var nowdata = new Date();
                var nowYear = nowdata.getFullYear();
                var nowMonth = nowdata.getMonth() + 1;
                var nowDay = nowdata.getDate();
                var nowdatastr = nowYear + nowMonth + nowDay;
                var datastr = year + month + day;
                if (nowdatastr == datastr) {
                    clock += hh + ":";
                    clock += mm;
                }
                else {
                    clock += month + "-";
                    clock += day + " ";
                    clock += hh + ":";
                    clock += mm;
                }
            }
                break;
        }

        return clock;

    },
	getQueryStringByName: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        var context = "";
        if (r != null)
            context = r[2];
        reg = null;
        r = null;
        return context == null || context == "" || context == "undefined" ? "" : context;
    },
    formatDate: {
        format: function (date, pattern) {
            pattern = pattern || ComFunJS.DEFAULT_PATTERN;
            return pattern.replace(ComFunJS.SIGN_REGEXP, function ($0) {
                switch ($0.charAt(0)) {
                    case 'y': return ComFunJS.padding(date.getFullYear(), $0.length);
                    case 'M': return ComFunJS.padding(date.getMonth() + 1, $0.length);
                    case 'd': return ComFunJS.padding(date.getDate(), $0.length);
                    case 'w': return date.getDay() + 1;
                    case 'h': return ComFunJS.padding(date.getHours(), $0.length);
                    case 'm': return ComFunJS.padding(date.getMinutes(), $0.length);
                    case 's': return ComFunJS.padding(date.getSeconds(), $0.length);
                }
            });
        },
        parse: function (dateString, pattern) {
            var matchs1 = pattern.match(SIGN_REGEXP);
            var matchs2 = dateString.match(/(\d)+/g);
            if (matchs1.length == matchs2.length) {
                var _date = new Date(1970, 0, 1);
                for (var i = 0; i < matchs1.length; i++) {
                    var _int = parseInt(matchs2[i]);
                    var sign = matchs1[i];
                    switch (sign.charAt(0)) {
                        case 'y': _date.setFullYear(_int); break;
                        case 'M': _date.setMonth(_int - 1); break;
                        case 'd': _date.setDate(_int); break;
                        case 'h': _date.setHours(_int); break;
                        case 'm': _date.setMinutes(_int); break;
                        case 's': _date.setSeconds(_int); break;
                    }
                }
                return _date;
            }
            return null;
        }

    },
    FilePath: function (fileid, wh) {
        fileid = fileid + "";
        if (fileid) {
            if (wh) {
                return "http://39.106.250.195:8011/api1/file/thum/" + fileid.split(',')[0] + wh;//缩略图,默认200*200，可以传宽高/200/200
            }
            else {
                return "http://39.106.250.195:8011/api1/file/thum/" + fileid.split(',')[0];
            }
        }
        else {
            return "";
        }
    },    
    saveToken: function (obj) {
        if (ComFunJS.getCookie("txcb962ac59075b964b0715")) {
            ComFunJS.delCookie("txcb962ac59075b964b0715");
        }
        ComFunJS.setCookie("txcb962ac59075b964b0715", obj, 30);
    },    
    setCookie: function (name, value, t) {  //设置cookie，默认保存7天
        var Days = 7;
        var exp = new Date();
        if (t) {
            exp.setTime(exp.getTime() + parseInt(t) * 24 * 60 * 60 * 1000);
        } else {
            exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        }
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
    },    
    getCookie: function (name) {//获取cookie
        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    },
    delCookie: function (name) { //删除cookie
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = ComFunJS.getCookie(name);
        if (cval != null) {
            document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/;";
        }
    },    //获取Token
    getToken: function () {
        return ComFunJS.getCookie("txcb962ac59075b964b0715");
    },
    delToken: function () {
       ComFunJS.delCookie("txcb962ac59075b964b0715");
    }	
}
export default ComFunJS;
